package com.uniriho.lxr0811_7300_bl.utils

import java.util.Locale

class DataTo {

    companion object {
        fun hexStrToByteArray(str: String?): ByteArray? {
            if (str == null) {
                return null
            }
            if (str.length == 0) {
                return ByteArray(0)
            }
            val byteArray = ByteArray(str.length / 2)
            for (i in byteArray.indices) {
                val subStr = str.substring(2 * i, 2 * i + 2)
                byteArray[i] = subStr.toInt(16).toByte()
            }
            return byteArray
        }

        fun byteArrayToHexStr(byteArray: ByteArray?): String? {
            if (byteArray == null) {
                return null
            }
            val hexArray = "0123456789ABCDEF".toCharArray()
            val hexChars = CharArray(byteArray.size * 2)
            for (j in byteArray.indices) {
                val v = byteArray[j].toInt() and 0xFF
                hexChars[j * 2] = hexArray[v ushr 4]
                hexChars[j * 2 + 1] = hexArray[v and 0x0F]
            }
            return String(hexChars)
        }
    }


}